package com.own.demo.entity;

/**
 * @Author zyf
 * @Date 2025-05-29 14:34
 * Description:
 * @Version 1.0
 */
public class LeftRightDeadLock {

    private final Object right = new Object();
    private final Object left = new Object();

    /**
     * 加锁顺序从left -> right
     */
    public void leftToRight() {
        synchronized (left) {
            synchronized (right) {
                System.out.println(Thread.currentThread().getName() + " left -> right lock.");
            }
        }
    }

    /**
     * 加锁顺序right -> left
     */
    public void rightToLeft() {
        synchronized (right) {
            synchronized (left) {
                System.out.println(Thread.currentThread().getName() + " right -> left lock.");
            }
        }
    }
}
